﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Temp;

namespace YArchitech.Plumbing
{
	public class HYSystemCal : HYObject, IHYXmlSerial, IDisposable
	{
		public string ShowResults { get; set; }

		public HashSet<HYPlumbingObject> Objects
		{
			get
			{
				return this.m_Objects;
			}
		}

		public HYPlumbingObject Root
		{
			get
			{
				return this.m_RootObject;
			}
		}

		public List<HYPipeCal> PipeCals
		{
			get
			{
				if (this.m_PipeCals == null)
				{
					if (this.systemType == HYSystemCal.SystemType.Drainage)
					{
						this.m_PipeCals = this.GetAllPipeCalsForDrain();
					}
					else if (this.systemType == HYSystemCal.SystemType.Spraying)
					{
						this.m_PipeCals = this.GetAllPipeCalsForSpray();
					}
					else if (this.systemType == HYSystemCal.SystemType.Fire)
					{
						this.m_PipeCals = this.GetAllPipeCalsForHydrant();
					}
					else
					{
						this.m_PipeCals = this.GetAllPipeCalsForDrain();
					}
				}
				return this.m_PipeCals;
			}
		}

		public bool CheckSelectSystem()
		{
			if (this.PipeCals == null)
			{
				YJKMessageBox.Warning("提取的系统不正确，所选管道应位于系统立管底端！\n同时要保证该系统完整。");
				return false;
			}
			if (this.systemType == HYSystemCal.SystemType.Drainage || this.systemType == HYSystemCal.SystemType.Feed)
			{
				if (this.GetAllBathroom().Count < 1)
				{
					YJKMessageBox.Warning("提取的系统不正确，所选管道应位于系统立管底端！\n同时要保证该系统完整。");
					return false;
				}
			}
			else if (this.systemType == HYSystemCal.SystemType.Fire && this.ListFires.Count < 1)
			{
				YJKMessageBox.Warning("提取的系统不正确，所选管道应位于系统立管底端！\n同时要保证该系统完整。");
				return false;
			}
			return true;
		}

		public HYPipeCal SprayStartPipe
		{
			get
			{
				if (this.sprayStartPipe == null)
				{
					this.sprayStartPipe = (this.m_RootObject as HYPipeCal);
				}
				return this.sprayStartPipe;
			}
		}

		public List<HYPipeCal> MainPipeCals
		{
			get
			{
				if (this.mainPipeCals == null)
				{
					this.mainPipeCals = this.GetMainPipeCalsForDrain();
				}
				return this.mainPipeCals;
			}
		}

		public List<HYFire> ListFires
		{
			get
			{
				if (this.listFire == null)
				{
					this.listFire = this.GetAllFire();
				}
				return this.listFire;
			}
		}

		public List<HYSprayNode> SprayNodes
		{
			get
			{
				if (this.sprayNodes == null)
				{
					this.sprayNodes = this.GetAllSprayNodes();
				}
				return this.sprayNodes;
			}
		}

		public bool IsEmpty
		{
			get
			{
				return this.m_Objects == null || this.m_Objects.Count == 0;
			}
		}

		public string Name { get; set; }

		public HYSystemCal()
		{
			this.m_Objects = new HashSet<HYPlumbingObject>();
		}

		public void Dispose()
		{
			this.m_Objects.Clear();
		}

		public int GetAllPipeCals(out List<HYPipeCal> PipeCals)
		{
			PipeCals = new List<HYPipeCal>();
			foreach (HYPlumbingObject hyplumbingObject in from pl in this.m_Objects
			where pl is HYPipeCal
			orderby pl.Index
			select pl)
			{
				PipeCals.Add(hyplumbingObject as HYPipeCal);
			}
			return PipeCals.Count;
		}

		public void CalDrain(bool bIsDes)
		{
			this.m_fluxCal.pipeCals = this.PipeCals;
			this.m_fluxCal.calParameter = this.m_calParameter;
			this.m_fluxCal.CalFiuxData(this.m_calDrain);
			this.m_fluxCal.Cal_DN_Grade(this, bIsDes);
		}

		public List<HYPipeCal> GetAllPipeCalsForDrain()
		{
			new List<HYPipeCal>();
			if (this.m_RootObject == null)
			{
				return null;
			}
			this.OnlyShowPipe(null, this.m_RootObject, null, null);
			this.SetPipesBaseCal(this.m_RootObject as HYPipeCal);
			return this.OrderThePipeAndSetTheNumber(this.m_RootObject);
		}

		public List<HYPipeCal> GetAllPipeCalsForHydrant()
		{
			List<HYPipeCal> list = new List<HYPipeCal>();
			if (this.m_RootObject == null)
			{
				return null;
			}
			this.OnlyShowPipe(null, this.m_RootObject, null, null);
			list = this.OrderThePipeAndSetTheNumber(this.m_RootObject);
			if (list == null)
			{
				return null;
			}
			foreach (HYPipeCal hypipeCal in list)
			{
				if (hypipeCal.perviousPipe != null && hypipeCal.perviousPipe.endFire != null && hypipeCal.endFire != null && hypipeCal.perviousPipe.endFire == hypipeCal.endFire)
				{
					hypipeCal.perviousPipe.endFire = null;
				}
			}
			return list;
		}

		public List<HYPipeCal> GetAllPipeCalsForSpray()
		{
			new List<HYPipeCal>();
			if (this.m_RootObject == null)
			{
				return null;
			}
			this.OnlyShowPipe(null, this.m_RootObject, null, null);
			(this.m_RootObject as HYPipeCal).bIsStart = true;
			return this.OrderPipesForSpray(this.m_RootObject as HYPipeCal);
		}

		protected List<HYPipeCal> OrderPipesForSpray(HYPipeCal startPipe)
		{
			if (startPipe == null)
			{
				return null;
			}
			List<HYPipeCal> list = new List<HYPipeCal>();
			string text = "F-";
			int num = 1;
			Dictionary<double, HYPipeCal> dictionary = null;
			this.GetEveryPipePathToEnd(startPipe, ref dictionary, 0.0);
			foreach (KeyValuePair<double, HYPipeCal> keyValuePair in dictionary)
			{
				HYPlumbingObject value = keyValuePair.Value;
				string str = text;
				int num2 = num;
				num = num2 + 1;
				value.Number = str + num2.ToString();
				list.Add(keyValuePair.Value);
				List<HYPipeCal> list2 = new List<HYPipeCal>();
				this.GetHYPipeCalByDiGui(keyValuePair.Value, ref list2, startPipe);
				foreach (HYPipeCal hypipeCal in list2)
				{
					if (!list.Contains(hypipeCal))
					{
						HYPlumbingObject hyplumbingObject = hypipeCal;
						string str2 = text;
						num2 = num;
						num = num2 + 1;
						hyplumbingObject.Number = str2 + num2.ToString();
						list.Add(hypipeCal);
					}
				}
			}
			startPipe.Number = "F-0";
			list.Add(startPipe);
			return list;
		}

		protected List<HYPipeCal> GetSubsequentPipes(HYPipeCal startPipe)
		{
			if (startPipe == null)
			{
				return null;
			}
			string text = "F-1-";
			int num = 1;
			List<HYPipeCal> list = new List<HYPipeCal>();
			Dictionary<double, HYPipeCal> dictionary = null;
			this.GetEveryPipePathToEnd(startPipe, ref dictionary, 0.0);
			foreach (KeyValuePair<double, HYPipeCal> keyValuePair in dictionary)
			{
				list.Add(keyValuePair.Value);
				List<HYPipeCal> list2 = new List<HYPipeCal>();
				this.GetHYPipeCalByDiGui(keyValuePair.Value, ref list2, startPipe);
				foreach (HYPipeCal hypipeCal in list2)
				{
					if (!list.Contains(hypipeCal))
					{
						HYPlumbingObject hyplumbingObject = hypipeCal;
						string str = text;
						int num2 = num;
						num = num2 + 1;
						hyplumbingObject.Number = str + num2.ToString();
						this.PipeCals.Add(hypipeCal);
					}
				}
			}
			return list;
		}

		protected int GetEveryPipePathToEnd(HYPipeCal previousPipe, ref Dictionary<double, HYPipeCal> pipePaths, double dLength = 0.0)
		{
			if (pipePaths == null)
			{
				pipePaths = new Dictionary<double, HYPipeCal>();
			}
			if (previousPipe == null)
			{
				return pipePaths.Count;
			}
			dLength += previousPipe.Length;
			foreach (HYPipeCal hypipeCal in previousPipe.subPipeArray)
			{
				dLength += hypipeCal.Length;
				if (hypipeCal.endSprinkler != null)
				{
					pipePaths.Add(dLength, hypipeCal);
					hypipeCal.endSprinkler.PathLength = dLength;
				}
				else
				{
					this.GetEveryPipePathToEnd(hypipeCal, ref pipePaths, dLength);
				}
			}
			List<double> list = new List<double>();
			foreach (KeyValuePair<double, HYPipeCal> keyValuePair in pipePaths)
			{
				list.Add(keyValuePair.Key);
			}
			list.Sort((double a, double b) => b.CompareTo(a));
			Dictionary<double, HYPipeCal> dictionary = new Dictionary<double, HYPipeCal>();
			foreach (double key in list)
			{
				dictionary.Add(key, pipePaths[key]);
			}
			pipePaths = dictionary;
			return pipePaths.Count;
		}

		public List<HYSprayNode> GetAllSprayNodes()
		{
			List<HYSprayNode> list = new List<HYSprayNode>();
			if (!this.statisticNode(this.SprayStartPipe, true, ref list) || list.Count == 0)
			{
				YJKMessageBox.Error("构造喷淋系统节点失败！");
			}
			return list;
		}

		protected bool statisticNode(HYPipeCal previousPipe, bool bStartPipe, ref List<HYSprayNode> nodes)
		{
			bool flag = true;
			HYSprayNode hysprayNode = null;
			if (bStartPipe)
			{
				HYSprayNode hysprayNode2 = new HYSprayNode();
				hysprayNode2.IsStart = true;
				hysprayNode2.OutPipes.Add(previousPipe);
				previousPipe.startNode = hysprayNode2;
				nodes.Add(hysprayNode2);
			}
			if (previousPipe.endSprinkler != null)
			{
				HYSprayNode hysprayNode3 = new HYSprayNode();
				hysprayNode3.sprinkler = previousPipe.endSprinkler;
				hysprayNode3.InPipes.Add(previousPipe);
				previousPipe.endNode = hysprayNode3;
				nodes.Add(hysprayNode3);
			}
			else
			{
				hysprayNode = new HYSprayNode();
				hysprayNode.InPipes.Add(previousPipe);
				previousPipe.endNode = hysprayNode;
				if (previousPipe.subPipeArray.Count > 0)
				{
					hysprayNode.OutPipes.AddRange(previousPipe.subPipeArray);
					foreach (HYPipeCal hypipeCal in previousPipe.subPipeArray)
					{
						hypipeCal.startNode = hysprayNode;
					}
				}
				nodes.Add(hysprayNode);
			}
			foreach (HYPipeCal previousPipe2 in previousPipe.subPipeArray)
			{
				flag &= this.statisticNode(previousPipe2, false, ref nodes);
			}
			return flag;
		}

		public List<HYPipeCal> GetCalculatedPipesForSpray()
		{
			List<List<HYPipeCal>> list = new List<List<HYPipeCal>>();
			foreach (HYPipeCal hypipeCal in this.PipeCals)
			{
				if (hypipeCal.endSprinkler != null && hypipeCal.endSprinkler.IsInArea)
				{
					List<HYPipeCal> list2 = new List<HYPipeCal>();
					list2.Add(hypipeCal);
					this.GetPreviousPipes(hypipeCal, list, ref list2);
					list.Add(list2);
				}
			}
			List<HYPipeCal> list3 = new List<HYPipeCal>();
			int num = 1;
			foreach (List<HYPipeCal> list4 in list)
			{
				foreach (HYPipeCal hypipeCal2 in list4)
				{
					hypipeCal2.iPathNum = num;
					list3.Add(hypipeCal2);
				}
				num++;
			}
			return list3;
		}

		protected bool PipeIsInSprayPaths(HYPipeCal pipe, List<List<HYPipeCal>> paths)
		{
			bool result = false;
			foreach (List<HYPipeCal> list in paths)
			{
				foreach (HYPipeCal hypipeCal in list)
				{
					if (list.Contains(pipe))
					{
						result = true;
					}
				}
			}
			return result;
		}

		public bool SetSelectedSprinkler(Element elem)
		{
			bool result = false;
			foreach (HYPipeCal hypipeCal in this.PipeCals)
			{
				if (hypipeCal.endSprinkler != null && hypipeCal.endSprinkler.Id == elem.Id.IntegerValue)
				{
					hypipeCal.endSprinkler.IsInArea = true;
					result = true;
					break;
				}
			}
			return result;
		}

		public void ClearSprinklerSelected()
		{
			foreach (HYPipeCal hypipeCal in this.PipeCals)
			{
				if (hypipeCal.endSprinkler != null)
				{
					hypipeCal.endSprinkler.IsInArea = false;
				}
			}
		}

		protected void GetPreviousPipes(HYPipeCal hyPipeCal, List<List<HYPipeCal>> paths, ref List<HYPipeCal> list)
		{
			if (hyPipeCal == null || hyPipeCal.perviousPipe == null)
			{
				return;
			}
			if (!this.PipeIsInSprayPaths(hyPipeCal.perviousPipe, paths) && !list.Contains(hyPipeCal.perviousPipe))
			{
				list.Add(hyPipeCal.perviousPipe);
				this.GetPreviousPipes(hyPipeCal.perviousPipe, paths, ref list);
			}
		}

		public void ResetPipeInfo()
		{
			foreach (HYSprayNode hysprayNode in this.SprayNodes)
			{
				hysprayNode.CalculateNum = -1;
				hysprayNode.Pressure = 0.0;
				hysprayNode.FLow = 0.0;
				hysprayNode.IsFixedPressure = false;
				hysprayNode.IsCalculated = false;
				foreach (HYPipeCal hypipeCal in hysprayNode.InPipes)
				{
					hypipeCal.Flow = 0.0;
					hypipeCal.startPress = 0.0;
					hypipeCal.iPathNum = -1;
					hypipeCal.Velocity = 0.0;
					hypipeCal.dCs = 0.0;
					hypipeCal.dEquivalent = 0.0;
				}
				foreach (HYPipeCal hypipeCal2 in hysprayNode.OutPipes)
				{
					hypipeCal2.Flow = 0.0;
					hypipeCal2.startPress = 0.0;
					hypipeCal2.iPathNum = -1;
					hypipeCal2.Velocity = 0.0;
					hypipeCal2.dCs = 0.0;
					hypipeCal2.dEquivalent = 0.0;
				}
			}
		}

		public List<HYPipeCal> GetMainPipeCalsForDrain()
		{
			new List<HYPipeCal>();
			if (this.m_RootObject == null)
			{
				return null;
			}
			this.OnlyShowPipe(null, this.m_RootObject, null, null);
			return this.OrderTheMainPipeAndSetTheNumber(this.m_RootObject);
		}

		public List<HYFire> GetAllFire()
		{
			List<HYFire> list = new List<HYFire>();
			foreach (HYPlumbingObject hyplumbingObject in this.m_Objects)
			{
				if (hyplumbingObject is HYFire)
				{
					list.Add(hyplumbingObject as HYFire);
				}
			}
			return list;
		}

		public List<HYBathroom> GetAllBathroom()
		{
			List<HYBathroom> list = new List<HYBathroom>();
			foreach (HYPlumbingObject hyplumbingObject in this.m_Objects)
			{
				if (hyplumbingObject is HYBathroom)
				{
					list.Add(hyplumbingObject as HYBathroom);
				}
			}
			return list;
		}

		public bool CheckAllBathroomIsDef()
		{
			IEnumerable<HYBathroom> allBathroom = this.GetAllBathroom();
			List<string> list = new List<string>();
			List<HYBathroom> list2 = (from vale in allBathroom
			where !vale.bDefinition
			select vale).ToList<HYBathroom>();
			if (list2.Count < 1)
			{
				return true;
			}
			int num = 0;
			string text = "有如下未定义卫浴，确定继续计算：\n";
			foreach (HYBathroom hybathroom in list2)
			{
				if (!list.Contains(hybathroom.szName))
				{
					num++;
					if (num > 5)
					{
						list.Add("......");
						text += "\n......";
						break;
					}
					list.Add(hybathroom.szName);
					text = text + "\n" + hybathroom.szName;
				}
			}
			return DialogResult.OK == MessageBox.Show(text, "盈建科提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
		}

		public void GetRootPipe(ref List<HYPipeCal> pipeCals, ref int rootnum, ref HYPipeCal firstPipe)
		{
			if (firstPipe.subPipeArray.Count == 0)
			{
				pipeCals = null;
				return;
			}
			if (firstPipe.subPipeArray.Count != 0 && firstPipe.subPipeArray[0].bIsHorL)
			{
				rootnum++;
				firstPipe = firstPipe.subPipeArray[0];
				firstPipe.Number = "R-" + rootnum.ToString();
				pipeCals.Add(firstPipe);
				this.GetRootPipe(ref pipeCals, ref rootnum, ref firstPipe);
			}
		}

		public List<HYPipeCal> OrderThePipeAndSetTheNumber(HYPlumbingObject Obj)
		{
			if (Obj == null)
			{
				return null;
			}
			List<HYPipeCal> list = new List<HYPipeCal>();
			List<HYPipeCal> list2 = new List<HYPipeCal>();
			List<HYPipeCal> listH = new List<HYPipeCal>();
			if (!(Obj as HYPipeCal).bIsHorL)
			{
				list2.Add(Obj as HYPipeCal);
				this.GetTheLPipeAndHPipe(ref list2, ref listH, Obj);
			}
			else
			{
				(Obj as HYPipeCal).Number = "R-1";
				int num = 1;
				list.Add(Obj as HYPipeCal);
				HYPipeCal obj = Obj as HYPipeCal;
				try
				{
					this.GetRootPipe(ref list, ref num, ref obj);
					this.GetTheLPipeAndHPipe(ref list2, ref listH, obj);
				}
				catch (Exception)
				{
					return null;
				}
			}
			if (list2.Count > 0)
			{
				int num2 = 1;
				foreach (HYPipeCal hypipeCal in list2)
				{
					if (this.IsUsableness(hypipeCal))
					{
						HYPlumbingObject hyplumbingObject = hypipeCal;
						string str = "L-";
						int num3 = num2;
						num2 = num3 + 1;
						hyplumbingObject.Number = str + num3.ToString();
						list.Add(hypipeCal);
					}
				}
			}
			this.AddNumberForHPipeInLevel(listH, ref list);
			return list;
		}

		public bool IsUsableness(HYPipeCal pipe)
		{
			if ((this.systemType == HYSystemCal.SystemType.Drainage || this.systemType == HYSystemCal.SystemType.Feed) && pipe.endBathroom != null)
			{
				return true;
			}
			if (this.systemType == HYSystemCal.SystemType.Fire && pipe.endFire != null)
			{
				return true;
			}
			foreach (HYPipeCal pipe2 in pipe.subPipeArray)
			{
				if (this.IsUsableness(pipe2))
				{
					return true;
				}
			}
			return false;
		}

		public List<HYPipeCal> OrderTheMainPipeAndSetTheNumber(HYPlumbingObject Obj)
		{
			if (Obj == null)
			{
				return null;
			}
			List<HYPipeCal> list = new List<HYPipeCal>();
			List<HYPipeCal> list2 = new List<HYPipeCal>();
			List<HYPipeCal> list3 = new List<HYPipeCal>();
			if (!(Obj as HYPipeCal).bIsHorL)
			{
				list2.Add(Obj as HYPipeCal);
				this.GetTheLPipeAndHPipe(ref list2, ref list3, Obj);
			}
			else
			{
				(Obj as HYPipeCal).Number = "R-1";
				int num = 1;
				list.Add(Obj as HYPipeCal);
				HYPipeCal obj = Obj as HYPipeCal;
				try
				{
					this.GetRootPipe(ref list, ref num, ref obj);
					this.GetTheLPipeAndHPipe(ref list2, ref list3, obj);
				}
				catch (Exception)
				{
				}
			}
			if (list2.Count > 0)
			{
				int num2 = 1;
				foreach (HYPipeCal hypipeCal in list2)
				{
					HYPlumbingObject hyplumbingObject = hypipeCal;
					string str = "L-";
					int num3 = num2;
					num2 = num3 + 1;
					hyplumbingObject.Number = str + num3.ToString();
					list.Add(hypipeCal);
				}
			}
			if (list3.Count > 0)
			{
				int num4 = 1;
				foreach (HYPipeCal hypipeCal2 in list3)
				{
					HYPlumbingObject hyplumbingObject2 = hypipeCal2;
					string str2 = "F-";
					int num3 = num4;
					num4 = num3 + 1;
					hyplumbingObject2.Number = str2 + num3.ToString();
					list.Add(hypipeCal2);
				}
			}
			return list;
		}

		public void AddNumberForHPipeInLevel(List<HYPipeCal> listH, ref List<HYPipeCal> pipeCals)
		{
			int num = 1;
			foreach (HYPipeCal hypipeCal in listH)
			{
				string str = "F-";
				int num2 = num;
				num = num2 + 1;
				string text = str + num2.ToString() + "-";
				int num3 = 1;
				Dictionary<double, HYPipeCal> dictionary = null;
				this.GetPipeSubBathRoompipe(hypipeCal, ref dictionary, 0.0);
				foreach (KeyValuePair<double, HYPipeCal> keyValuePair in dictionary)
				{
					if (keyValuePair.Value.endBathroom == null)
					{
						HYPlumbingObject value = keyValuePair.Value;
						string str2 = text;
						num2 = num3;
						num3 = num2 + 1;
						value.Number = str2 + num2.ToString();
						pipeCals.Add(keyValuePair.Value);
					}
					List<HYPipeCal> list = new List<HYPipeCal>();
					this.GetHYPipeCalByDiGui(keyValuePair.Value, ref list, hypipeCal);
					foreach (HYPipeCal hypipeCal2 in list)
					{
						if (!pipeCals.Contains(hypipeCal2))
						{
							HYPlumbingObject hyplumbingObject = hypipeCal2;
							string str3 = text;
							num2 = num3;
							num3 = num2 + 1;
							hyplumbingObject.Number = str3 + num2.ToString();
							pipeCals.Add(hypipeCal2);
						}
					}
				}
				hypipeCal.Number = "F-" + (num - 1).ToString();
				pipeCals.Add(hypipeCal);
			}
		}

		public void GetHYPipeCalByDiGui(HYPipeCal hyPipeCal, ref List<HYPipeCal> list, HYPipeCal item)
		{
			if (hyPipeCal == null)
			{
				return;
			}
			if (hyPipeCal.perviousPipe.Id != item.Id && !list.Contains(hyPipeCal.perviousPipe))
			{
				list.Add(hyPipeCal.perviousPipe);
				this.GetHYPipeCalByDiGui(hyPipeCal.perviousPipe, ref list, item);
			}
		}

		public void GetTheLPipeAndHPipe(ref List<HYPipeCal> listL, ref List<HYPipeCal> listH, HYPlumbingObject Obj)
		{
			if ((Obj as HYPipeCal).subPipeArray.Count != 2)
			{
				if ((Obj as HYPipeCal).subPipeArray.Count == 1)
				{
					if ((Obj as HYPipeCal).subPipeArray[0].bIsHorL)
					{
						listH.Add((Obj as HYPipeCal).subPipeArray[0]);
						return;
					}
					listL.Add((Obj as HYPipeCal).subPipeArray[0]);
					this.GetTheLPipeAndHPipe(ref listL, ref listH, (Obj as HYPipeCal).subPipeArray[0]);
				}
				return;
			}
			if (this.systemType == HYSystemCal.SystemType.Fire && (Obj as HYPipeCal).subPipeArray[0].bIsHorL && (Obj as HYPipeCal).subPipeArray[1].bIsHorL)
			{
				return;
			}
			if (!(Obj as HYPipeCal).subPipeArray[0].bIsHorL)
			{
				listL.Add((Obj as HYPipeCal).subPipeArray[0]);
				listH.Add((Obj as HYPipeCal).subPipeArray[1]);
				this.GetTheLPipeAndHPipe(ref listL, ref listH, (Obj as HYPipeCal).subPipeArray[0]);
				return;
			}
			listH.Add((Obj as HYPipeCal).subPipeArray[0]);
			if (!(Obj as HYPipeCal).subPipeArray[1].bIsHorL)
			{
				listL.Add((Obj as HYPipeCal).subPipeArray[1]);
				this.GetTheLPipeAndHPipe(ref listL, ref listH, (Obj as HYPipeCal).subPipeArray[1]);
				return;
			}
			listH.Add((Obj as HYPipeCal).subPipeArray[1]);
		}

		public void OnlyShowPipe(HYPipeCal perpipe, HYPlumbingObject Obj, List<HYFittingCal> fittings = null, List<HYPipeValveCal> valves = null)
		{
			if (Obj == null)
			{
				return;
			}
			if (perpipe != null)
			{
				if (this.systemType == HYSystemCal.SystemType.Drainage)
				{
					if (Obj is HYPipeCal)
					{
						(Obj as HYPipeCal).Init();
						perpipe.AddSubPipe(Obj as HYPipeCal);
						perpipe = (Obj as HYPipeCal);
					}
					else if (Obj is HYFittingCal)
					{
						perpipe.AddFitting(Obj as HYFittingCal);
					}
					else if (Obj is HYBathroom)
					{
						perpipe.endBathroom = (Obj as HYBathroom);
					}
					else if (Obj is HYPipeValveCal)
					{
						perpipe.AddValve(Obj as HYPipeValveCal);
					}
				}
				else if (this.systemType == HYSystemCal.SystemType.Spraying)
				{
					if (Obj is HYPipeCal)
					{
						(Obj as HYPipeCal).Init();
						if (fittings != null)
						{
							foreach (HYFittingCal fit in fittings)
							{
								(Obj as HYPipeCal).AddFitting(fit);
							}
							fittings.Clear();
						}
						if (valves != null)
						{
							foreach (HYPipeValveCal valve in valves)
							{
								(Obj as HYPipeCal).AddValve(valve);
							}
							valves.Clear();
						}
						if (Obj.Previous is HYFittingCal && Obj.subObjectArray.Count == 1)
						{
							HYSprinklerCal hysprinklerCal = Obj.subObjectArray[0] as HYSprinklerCal;
						}
						perpipe.AddSubPipe(Obj as HYPipeCal);
						perpipe = (Obj as HYPipeCal);
					}
					else if (Obj is HYFittingCal)
					{
						if (fittings == null)
						{
							fittings = new List<HYFittingCal>();
						}
						fittings.Add(Obj as HYFittingCal);
					}
					else if (Obj is HYPipeValveCal)
					{
						if (fittings == null)
						{
							fittings = new List<HYFittingCal>();
						}
						fittings.Add(Obj as HYFittingCal);
					}
					else if (Obj is HYSprinklerCal)
					{
						perpipe.endSprinkler = (Obj as HYSprinklerCal);
					}
				}
				else if (Obj is HYPipeCal)
				{
					(Obj as HYPipeCal).Init();
					if (fittings != null)
					{
						foreach (HYFittingCal fit2 in fittings)
						{
							(Obj as HYPipeCal).AddFitting(fit2);
						}
						fittings.Clear();
					}
					if (valves != null)
					{
						foreach (HYPipeValveCal valve2 in valves)
						{
							(Obj as HYPipeCal).AddValve(valve2);
						}
						valves.Clear();
					}
					perpipe.AddSubPipe(Obj as HYPipeCal);
					perpipe = (Obj as HYPipeCal);
				}
				else if (Obj is HYFittingCal)
				{
					if (fittings == null)
					{
						fittings = new List<HYFittingCal>();
					}
					fittings.Add(Obj as HYFittingCal);
				}
				else if (Obj is HYBathroom)
				{
					perpipe.endBathroom = (Obj as HYBathroom);
				}
				else if (Obj is HYFire)
				{
					perpipe.endFire = (Obj as HYFire);
					perpipe.endBathroom = new HYBathroom();
					if (perpipe.perviousPipe != null)
					{
						perpipe.perviousPipe.endFire = perpipe.endFire;
					}
				}
				else if (Obj is HYPipeValveCal)
				{
					if (valves == null)
					{
						valves = new List<HYPipeValveCal>();
					}
					valves.Add(Obj as HYPipeValveCal);
				}
			}
			else
			{
				perpipe = (Obj as HYPipeCal);
			}
			foreach (HYPlumbingObject obj in Obj.subObjectArray)
			{
				List<HYFittingCal> list = null;
				if (fittings != null && fittings.Count > 0)
				{
					list = new List<HYFittingCal>();
					list.AddRange(fittings);
				}
				List<HYPipeValveCal> list2 = null;
				if (valves != null && valves.Count > 0)
				{
					list2 = new List<HYPipeValveCal>();
					list2.AddRange(valves);
				}
				this.OnlyShowPipe(perpipe, obj, list, list2);
			}
		}

		public int GetPipeSubBathRoompipe(HYPipeCal pipe, ref Dictionary<double, HYPipeCal> dicpipe, double dLen = 0.0)
		{
			if (dicpipe == null)
			{
				dicpipe = new Dictionary<double, HYPipeCal>();
			}
			if (pipe == null)
			{
				return dicpipe.Count;
			}
			dLen += pipe.Length;
			foreach (HYPipeCal hypipeCal in pipe.subPipeArray)
			{
				dLen += hypipeCal.Length;
				if (hypipeCal.endBathroom != null)
				{
					dicpipe.Add(dLen, hypipeCal);
				}
				else
				{
					this.GetPipeSubBathRoompipe(hypipeCal, ref dicpipe, dLen);
				}
			}
			List<double> list = new List<double>();
			foreach (KeyValuePair<double, HYPipeCal> keyValuePair in dicpipe)
			{
				list.Add(keyValuePair.Key);
			}
			list.Sort((double a, double b) => b.CompareTo(a));
			Dictionary<double, HYPipeCal> dictionary = new Dictionary<double, HYPipeCal>();
			foreach (double key in list)
			{
				dictionary.Add(key, dicpipe[key]);
			}
			dicpipe = dictionary;
			return dicpipe.Count;
		}

		public void SetPipesBaseCal(HYPipeCal pipe)
		{
			if (pipe == null)
			{
				return;
			}
			pipe.upBathroomArray.Clear();
			pipe.MinDN = 0;
			pipe.bIsCal = false;
			if (pipe.endBathroom != null)
			{
				pipe.upBathroomArray.Add(pipe.endBathroom);
				pipe.MinDN = pipe.nDN;
				pipe.bIsCal = false;
			}
			for (int i = 0; i < pipe.subPipeArray.Count; i++)
			{
				this.SetPipesBaseCal(pipe.subPipeArray[i]);
				if (pipe.subPipeArray[i].upBathroomArray.Count > 0)
				{
					pipe.upBathroomArray.AddRange(pipe.subPipeArray[i].upBathroomArray);
				}
				if (pipe.MinDN < pipe.subPipeArray[i].MinDN)
				{
					pipe.MinDN = pipe.subPipeArray[i].MinDN;
				}
				if (pipe.subPipeArray[i].bIsCal)
				{
					pipe.bIsCal = true;
				}
			}
			if (pipe.subPipeArray.Count > 1)
			{
				pipe.bIsCal = true;
			}
		}

		public void Reomve(HYPlumbingObject hyObj)
		{
			this.m_Objects.Remove(hyObj);
		}

		public void SetRootObject(HYPlumbingObject hyObj)
		{
			this.m_RootObject = hyObj;
			this.Add(hyObj);
		}

		public bool Add(HYPlumbingObject hyObj)
		{
			return this.m_Objects.Add(hyObj);
		}

		public HYPlumbingObject FindObjectById(int id)
		{
			return this.m_Objects.First((HYPlumbingObject hyObj) => hyObj.Id == id);
		}

		public HYPlumbingObject FindObjectByIndex(int index)
		{
			return this.m_Objects.First((HYPlumbingObject hyObj) => hyObj.Index == index);
		}

		public Document doc;

		public HYSystemCal.SystemType systemType;

		protected HashSet<HYPlumbingObject> m_Objects;

		protected HYPlumbingObject m_RootObject;

		public CalDrainData m_calDrain = new CalDrainData();

		public HYSupplyFormula supplyParameter = new HYSupplyFormula();

		public HYCalParameter m_calParameter = new HYCalParameter();

		private List<HYPipeCal> m_PipeCals;

		private List<HYPipeCal> mainPipeCals;

		private List<HYFire> listFire;

		private List<HYSprayNode> sprayNodes;

		private HYPipeCal sprayStartPipe;

		public HYFluxCal m_fluxCal = new HYFluxCal();

		public HYFireData hyFireData = new HYFireData();

		public enum SystemType
		{
			Drainage,
			Feed,
			Fire,
			Spraying,
			UnDefine
		}
	}
}
